//
//  TabBar.swift
//  YueChat
//
//  Created by IMac on 2024/9/12.
//

import UIKit

/**
 *  RGBA颜色
 */
func ColorRGBA(_ r:CGFloat,g:CGFloat,b:CGFloat,a:CGFloat) -> UIColor {
    
    return UIColor(red:r/255.0,green:g/255.0,blue:b/255.0,alpha:a)
}
/**
 *  RGB颜色
 */
func ColorRGB(_ r:CGFloat,g:CGFloat,b:CGFloat) -> UIColor {
    
    return ColorRGBA(r, g: g, b: b, a: 1.0)
}
/**
 *  随机色
 */
func ColorRandom() -> UIColor {
    
    return ColorRGB(CGFloat(arc4random()%255), g: CGFloat(arc4random()%255), b: CGFloat(arc4random()%255))
}
/**
 *  屏幕宽度
 */
private let MWIDTH = UIScreen.main.bounds.size.width

/**
 *  屏幕高度
 */
private let MHEIGHT = UIScreen.main.bounds.size.height

/**
 *  tabbar背景色
 */
private let ColorTabBar = UIColor.white

/**
 *  title默认颜色
 */
private let ColorTitle = UIColor.gray

/**
 *  title选中颜色
 */
private let ColorTitleSel = kMainColor//ColorRGB(41,g: 167,b: 245)

/**
 *  title字体大小
 */
private let titleFontSize : CGFloat = 12.0

/**
 *  数字角标直径
 */
private let numMarkD:CGFloat = 20.0

/**
 *  小红点直径
 */
private let pointMarkD:CGFloat = 10.0

/**
 *  button 图片与文字上下占比
 */
private let scale:CGFloat = 0.65


extension TabBar{
    
    /**
     *  切换显示控制器
     *
     *  - param: index 位置
     */
    public func showControllerIndex(_ index: Int) {
        
        guard index < controllerArray.count else
        {
            print("error:index="+"\(index)"+"超出范围")
            return;
        }
        self.seleBtn!.isSelected = false
        let button = (cusTabbar.viewWithTag(1000+index) as? UIButton)!
        button.isSelected = true
        self.seleBtn = button
        self.selectedIndex = index
    }
    
    /**
     *  设置数字角标
     *
     *  - param: num   所要显示数字
     *  - param: index 位置
     */
    public func showBadgeMark(_ badge: Int, index: Int) {
        
        guard index < controllerArray.count else
        {
            print("error:index="+"\(index)"+"超出范围")
            return;
        }
        
        let numLabel = (cusTabbar.viewWithTag(1020+index) as? UILabel)!
        numLabel.isHidden = false
        var nFrame = numLabel.frame
        if badge <= 0 {
            //隐藏角标
            self.hideMarkIndex(index)
            
        } else {
            
            if badge > 0 && badge <= 9 {
                
                nFrame.size.width = numMarkD
                
            } else if badge > 9 && badge <= 19 {
                
                nFrame.size.width = numMarkD+5
                
            } else {
                
                nFrame.size.width = numMarkD+10
                
            }
            nFrame.size.height = numMarkD
            numLabel.frame = nFrame
            numLabel.layer.cornerRadius = numMarkD/2.0
            numLabel.text = "\(badge)"
            if badge > 99 {
                numLabel.text = "99+"
            }
            
        }
    }
    
    /**
     *  设置小红点
     *
     *  - param: index 位置
     */
    public func showPointMarkIndex(_ index: Int) {
        guard index < controllerArray.count else
        {
            print("error:index="+"\(index)"+"超出范围")
            return;
        }
        let numLabel = (cusTabbar.viewWithTag(1020+index) as? UILabel)!
        numLabel.isHidden = false
        var nFrame = numLabel.frame
        nFrame.size.height = pointMarkD
        nFrame.size.width = pointMarkD
        numLabel.frame = nFrame
        numLabel.layer.cornerRadius = pointMarkD/2.0
        numLabel.text = ""
    }
    
    /**
     *  影藏指定位置角标
     *
     *  - param: index 位置
     */
    public func hideMarkIndex(_ index: Int) {
        guard index < controllerArray.count else
        {
            print("error:index="+"\(index)"+"超出范围")
            return;
        }
        let numLabel = (cusTabbar.viewWithTag(1020+index) as? UILabel)!
        numLabel.isHidden = true
    }
    
}
//MARK: - TabBarButton
class TabBarButton:UIButton {
    
    override var isHighlighted: Bool{
        
        didSet{
            super.isHighlighted = false
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        imageView?.contentMode = UIView.ContentMode.scaleAspectFit
        titleLabel?.textAlignment = NSTextAlignment.center
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
        let newX:CGFloat = 0.0
        let newY:CGFloat = 2.0
        let newWidth:CGFloat = CGFloat(contentRect.size.width)
        let newHeight:CGFloat = CGFloat(contentRect.size.height)*scale-newY
        return CGRect(x: newX, y: newY, width: newWidth, height: newHeight)
    }
    
    override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
        let newX: CGFloat = 0
        let newY: CGFloat = contentRect.size.height*scale
        let newWidth: CGFloat = contentRect.size.width
        let newHeight: CGFloat = contentRect.size.height-contentRect.size.height*scale
        return CGRect(x: newX, y: newY, width: newWidth, height: newHeight)
    }
    
}

//MARK: - TabBarController
open class TabBar:UITabBarController {
    
    var seleBtn: UIButton?
    var tabBarHeight:CGFloat = 49.0
    var titleArray = [String]()
    var imageArray = [String]()
    var selImageArray = [String]()
    var controllerArray = [String]()
    
    public init() {
//    public init() {
        
        
        let  controllerArray = ["HomeVC","ContactVC","MyVC"]

        let  titleArray = ["悦聊","通讯录","我"]

        let  imageArray = ["msg_tabbar","friend_tabbar","me_tabbar"]

        let  selImageArray = ["msg_tabbar_sel","friend_tabbar_sel","me_tabbar_sel"]
        
//        let  controllerArray = ["WebVC","HomeVC","ContactVC","MyVC"]
//
//        let  titleArray = ["首页","悦聊","通讯录","我"]
//
//        let  imageArray = ["home_tabbar","msg_tabbar","friend_tabbar","me_tabbar"]
//
//        let  selImageArray = ["home_tabbar_sel","msg_tabbar_sel","friend_tabbar_sel","me_tabbar_sel"]

        let height = CGFloat(49)
        
        self.controllerArray = controllerArray
        self.titleArray = titleArray
        self.imageArray = imageArray
        self.selImageArray = selImageArray
        
        tabBarHeight = height;
        
        var PhoneX = false
        guard #available(iOS 11.0, *) else {
            PhoneX = false // iOS 11之前没有刘海屏设备
        }
        
        if let safeArea = UIApplication.shared.keyWindow?.safeAreaInsets.top, safeArea > 0 {
            PhoneX = true
        }
        
        if PhoneX {
            tabBarHeight = 49.0 + 34.0
        }else{
            tabBarHeight = 49.0
        }
        
        super.init(nibName: nil, bundle: nil)
    }
    
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override open func viewDidLoad() {
        super.viewDidLoad()
        
        addController()
        self.tabBar.addSubview(cusTabbar)
        addTabBarButton()
        setupTabbarLine()
        
        geventService.addObserver(OsnEvent.KickOffAction, "KickOffAction") { [weak self]  data in
            guard let self = self else {return}
            self.loginOutKikoffUpdated()
        }
        
        geventService.addObserver(OsnEvent.kTokenError, "kTokenError") { data in
            InfoManager.shared.loginOut()
        }
        
        
        let isLogin = gosnLogin.isLogined()
        if isLogin {
            self.requestToken()
        }
    }
    
    override open func viewWillLayoutSubviews() {
        
        super.viewWillLayoutSubviews()
        self.removeTabBarButton()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     添加控制器
     */
    fileprivate func addController(){
        
        guard controllerArray.count > 0 else
        {
            print("error:控制器数组为nil")
            return
        }
        
        var navArray = [UIViewController]()
        //获取命名空间
        let ns = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        
        for (index, className) in controllerArray.enumerated() {
            
            // 将类名转化为类
            let cls: AnyClass? = NSClassFromString(ns + "." + className)
            
            //Swift中如果想通过一个Class来创建一个对象, 必须告诉系统这个Class的确切类型
            guard let vcCls = cls as? UIViewController.Type else
            {
                print("error:cls不能当做UIViewController")
                return
            }
            let vc = vcCls.init()
            vc.title = titleArray[index]
            let nav = YLNavigationVC(rootViewController:vc)
            navArray.append(nav)
        }
        
        viewControllers  = navArray;
    }
    
    @objc func loginOutKikoffUpdated() {
//        let savedToken = UserDefaults.standard.string(forKey: "mhhy_token")
//        let savedUserId = UserDefaults.standard.string(forKey: "mhhy_id")
//
//        if let token = savedToken, let userId = savedUserId, !token.isEmpty && !userId.isEmpty {
        GK_main_async_safe {
            MHAlert.showCustomAlert(KLocalizedString("AlertSureRefreshLogin")) { ac in
                if ac.title == KLocalizedString("Sure") {
                    InfoManager.shared.loginOut()
                }
            }
        }
//        }
    }
    
    func requestToken() {
//        guard let login = InfoManager.shared.currentInfo,
//              let refreshToken = login.refreshToken else {
//            return
//        }
        var login = gosnLogin.loadLoginData()
        let refreshToken = login["refreshToken"] as? String
        print("[FW]refreshToken:\(String(describing: refreshToken))")
        Task {
            let (res, err) = await ghttpService.refreshToken(refreshToken!)
            if let err = err {
                GK_main_async_safe {
                    MHAlert.showMessage("\(err.description)")
                    InfoManager.shared.loginOut()
                }
                
            }else{
                print("[FW]2refreshToken:\(String(describing: res!["refreshToken"]))")
                login["refreshToken"] = res!["refreshToken"]
                login["token"] = res!["token"]
                gosnLogin.saveLoginData(login)
                ghttpService.setConfig(login)
            }
        }
    }
    
    /**
     *  移除系统创建的UITabBarButton
     */
    fileprivate func removeTabBarButton()
    {
        for view in tabBar.subviews {
            if view.isKind(of: NSClassFromString("UITabBarButton")!) {
                view.removeFromSuperview()
            }
        }
        
    }
    
    /**
     添加tabbarButton
     */
    fileprivate func addTabBarButton()
    {
        
        let num = controllerArray.count
        for i in 0..<num {
            
            let  width = UIScreen.main.bounds.size.width
            let  x = CGFloat(width/CGFloat(num)*CGFloat(i))
            let  y:CGFloat = 10.0
            let  w = width/CGFloat(num)
            let  h = 49.0
            let button = TabBarButton(frame:CGRect(x: x,y: y,width: w,height: h))
            
            button.tag = 1000+i
            button.setTitleColor(ColorTitle, for: UIControl.State())
            button.setTitleColor(ColorTitleSel, for: UIControl.State.selected)
            button.titleLabel?.font = UIFont.systemFont(ofSize: titleFontSize)
            button.setImage(UIImage.init(named:self.imageArray[i]), for: UIControl.State())
            button.setImage(UIImage.init(named: self.selImageArray[i]), for: UIControl.State.selected)
            button.setTitle(self.titleArray[i], for: UIControl.State())
            button.addTarget(self, action:#selector(buttonAction(_:)), for: .touchDown)
            
            cusTabbar.addSubview(button)
            
            //默认选中
            if i == 0 {
                
                button.isSelected = true
                self.seleBtn = button
            }
            
            //角标
            let numLabel = UILabel(frame: CGRect(x: button.frame.size.width/2.0+8, y: 0, width: numMarkD, height: numMarkD))
            numLabel.layer.masksToBounds = true
            numLabel.layer.cornerRadius = 10
            numLabel.backgroundColor = UIColor.red
            numLabel.textColor = UIColor.white
            numLabel.textAlignment = NSTextAlignment.center
            numLabel.font = UIFont.systemFont(ofSize: 13)
            numLabel.tag = 1020+i
            numLabel.isHidden = true
            button.addSubview(numLabel)
        }
    }
    
    
    /**
     处理高度>49 tabbar顶部线
     */
    fileprivate func setupTabbarLine()
    {
        guard tabBarHeight > 49 else
        {
            return;
        }
        self.tabBar.shadowImage = UIImage.init()
        self.tabBar.backgroundImage = UIImage.init()
        let line = UILabel(frame: CGRect(x: 0, y: 0,width: MWIDTH, height: 0.5))
        line.backgroundColor = UIColor.lightGray
        line.alpha = 0.2
        cusTabbar.addSubview(line)
    }
    
    
    //MARK: - Action
    @objc fileprivate func buttonAction(_ button: UIButton) {
        let index: Int = button.tag-1000
        self.showControllerIndex(index)
    }
    
    //MARK: - 懒加载
    fileprivate lazy var cusTabbar: UIView = {
        
        let x = CGFloat(0)
        let y = CGFloat(0)
        let width = MWIDTH
        let height = self.tabBarHeight
        
        let view = UIView(frame:CGRect(x: x,y: y,width: width,height: height))
        view.backgroundColor = ColorTabBar
        
        return view
    }()
    
    deinit {
//        NotificationCenter.default.addObserver(self,
//                                                       selector: #selector(loginOutKikoffUpdated),
//                                                       name: NSNotification.Name(OsnEvent.KickOffAction.rawValue ),
//                                                       object: nil)
        
        geventService.removeObserver(OsnEvent.kTokenError, "kTokenError")
        geventService.removeObserver(OsnEvent.KickOffAction, "KickOffAction")
    }
    
}


//extension UITabBar {
//    // 显示红点
//    func showBadgeOnItemIndex(_ index: Int) {
//        removeBadgeOnItemIndex(index)
//        
//        let bview = UIView()
//        bview.tag = 888 + index
//        bview.layer.cornerRadius = 4
//        bview.clipsToBounds = true
//        bview.backgroundColor = .red
//        
//        let tabFrame = bounds
//        
//        let value: CGFloat = {
//            switch index {
//            case 0:
//                return 0.56
//            case 1:
//                return 0.55
//            default:
//                return 0.52
//            }
//        }()
//        
//        let tabBarItemCount: CGFloat = {
////            let login = MHHYInfoManager.shared.currentInfo
////            if login.loginConfig.MainDapp.count > 0 {
////                return 4.0
////            } else {
//                return 3.0
////            }
//        }()
//        
//        let percentX = (CGFloat(index) + value) / tabBarItemCount
//        let x = ceil(percentX * tabFrame.width)
//        let y = ceil(0.1 * tabFrame.height)
//        bview.frame = CGRect(x: x, y: y, width: 8, height: 8)
//        
//        addSubview(bview)
//        bringSubviewToFront(bview)
//    }
//
//    // 隐藏红点
//    func hideBadgeOnItemIndex(_ index: Int) {
//        removeBadgeOnItemIndex(index)
//    }
//
//    // 移除控件
//    func removeBadgeOnItemIndex(_ index: Int) {
//        subviews.forEach { subview in
//            if subview.tag == 888 + index {
//                subview.removeFromSuperview()
//            }
//        }
//    }
//}
